package guis;

import exceptions.RepositoryException;
import guis.classroom.ClassroomPanel;
import guis.member.MemberPanel;
import guis.visitant.VisitantPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import communication.Facade;

public class MainWindow extends Window
{
	private static final long	serialVersionUID	= -5561309037290085478L;

	private Facade			facade;

	private JMenuBar		menuBar;
	private JMenu			fileMenu;
	private JMenu			reportMenu;
	private ClassroomPanel	classroomPanel;
	private MemberPanel		memberPanel;
	private VisitantPanel	visitantPanel;

	public MainWindow( String title ) throws RepositoryException
	{
		super( title );
		this.facade = new Facade();
		initialize();
		createPanels();
		createMenuBar();
		createMenus();
		createFileMenuItems();
		createReportMenuItems();
		this.setVisible( true );
	}
	
	private void bringToFront( JPanel panel )
	{
		classroomPanel.setVisible( false );
		visitantPanel.setVisible( false );
		
		panel.setVisible( true );
	}

	private void initialize()
	{
		this.setSize( 800, 600 );
		this.setLocationRelativeTo( null );
	}

	private void createMenuBar()
	{
		this.menuBar = ComponentFactory.createJMenuBar( this );
	}

	private void createMenus()
	{
		this.fileMenu = ComponentFactory.createJMenu( "Arquivo", menuBar );
		this.reportMenu = ComponentFactory.createJMenu( "Relatório", menuBar );
	}

	private void createFileMenuItems()
	{
		ComponentFactory.createJMenuItem( "Classes", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				bringToFront( classroomPanel );
			}

		}, KeyEvent.VK_C, fileMenu );

		ComponentFactory.createJMenuItem( "Membros", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				bringToFront( memberPanel );
			}

		}, KeyEvent.VK_M, fileMenu );

		ComponentFactory.createJMenuItem( "Visitantes", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				bringToFront( visitantPanel );
			}

		}, KeyEvent.VK_V, fileMenu );

		ComponentFactory.createJMenuItem( "Chamada", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				System.exit( 0 );
			}

		}, fileMenu );

		ComponentFactory.createJMenuItem( "Sair", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				System.exit( 0 );
			}

		}, fileMenu );

		fileMenu.insertSeparator( 3 );
		fileMenu.insertSeparator( 5 );
	}

	private void createReportMenuItems()
	{
		ComponentFactory.createJMenuItem( "Geral", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				System.exit( 0 );
			}

		}, reportMenu );

		ComponentFactory.createJMenuItem( "Por Classe", new ActionListener()
		{
			public void actionPerformed( ActionEvent arg0 )
			{
				System.exit( 0 );
			}

		}, reportMenu );
	}

	private void createPanels()
	{
		this.classroomPanel = new ClassroomPanel( this, facade );
		this.visitantPanel = new VisitantPanel( this, facade );
		this.memberPanel = new MemberPanel( this, facade );
		
		this.add( classroomPanel );
		this.add( visitantPanel );
		this.add( memberPanel );
	}
}
